package com.neusoft.neumooc.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.core.util.FileUtils;


/** 
 * @ClassName: UpAndDownController 
 * @Description: TODO 单个上传/删除图片
 * @author zls
 * @date 2016年11月3日
 */
@Controller
@RequestMapping(value = "/photo")
public class UpAndDownController extends BaseController{
	
	
	/** 
	 * @Title: upload 
	 * @Description: TODO 上传
	 * @param request
	 * @param response
	 * @param file 文件名
	 * @param path 路径
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/upload")
	@ResponseBody
	public Map<String, Object> upload(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Map<String, Object> retMap = new HashMap<String, Object>();
		Boolean flag = false;
		try {
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
			if (multipartResolver.isMultipart(request)) {
				MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
				Iterator<String> iter = multipartRequest.getFileNames();
				while (iter.hasNext()) {
					MultipartFile file = multipartRequest.getFile((String) iter.next());// 判断上传文件类型是否允许上传
					if (FileUtils.isImageFile(file)) {
						String pic_path = request.getSession().getServletContext().getRealPath("/resources/upload/uploadimg");
						String originalFilename = file.getOriginalFilename();
						String newfileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
						File localfile = new File(pic_path + "\\/" + newfileName);
						try {
							file.transferTo(localfile);//
							flag = true;
							retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
							retMap.put(Params.RET_MSG, "上传成功");
							retMap.put("imageName", newfileName);
						} catch (IllegalStateException | IOException e) {
							log.error("上传文件有异常信息抛出:" + e.getMessage(),e.getCause());
							retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
							retMap.put(Params.RET_MSG, "上传失败");
							retMap.put("imageName", newfileName);
						}
					}
				}
			}
			if (flag == false) {
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_MSG, "上传失败");
			}
		} catch (Exception e) {
			log.error("上传有异常信息抛出:" + e.getMessage(),e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "上传失败");
		}
		return retMap;
	}
	
	
	/** 
	 * @Title: delete 
	 * @Description: TODO 删除
	 * @param request
	 * @param response
	 * @param file 文件名
	 * @param path 路径
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request, HttpServletResponse response,String file,String path) throws Exception{
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			File targetFile = new File(request.getSession().getServletContext().getRealPath(path + "/" + file + ".jpg"));
			targetFile.delete();
			retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
			retMap.put(Params.RET_MSG, "删除成功");
		} catch (Exception e) {
			log.error("上传有异常信息抛出:" + e.getMessage(),e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "删除失败");
		}
		return retMap;
	}
	
}
